package com.resou.reader.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;

@SuppressLint("ClickableViewAccessibility")
public class DialogUtil {

    private Context mContext;

    public DialogUtil(Context context) {
        mContext = context;
    }

    /**
     * 自定义普通dialog
     */
    private AlertDialog customDialog;


    /**
     * 显示自定义删除Dialog
     *
     * @param str1             Dialog标题
     * @param str2             Dialog内容1
     * @param str3             Dialog内容2
     * @param onClickListener2 确定按钮监听
     * @param flag             true：显示取消按钮，false：隐藏取消按钮
     */
    public void showDialog(String str1, String str2,
                           String str3,
                           View.OnClickListener onClickListener1,
                           View.OnClickListener onClickListener2, boolean flag) {
        if (mContext == null || ((Activity) mContext).isFinishing()) {
            return;
        }
        if (customDialog == null) {
            customDialog = new AlertDialog.Builder(mContext).create();

            customDialog.setCanceledOnTouchOutside(true);
            customDialog.setCancelable(true);
            customDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                                     KeyEvent event) {
                    return true;
                }
            });
            customDialog.show();
        } else {
            customDialog.show();
        }
        if (customDialog != null) {
            Window view = customDialog.getWindow();
            if (view == null) {
                return;
            }
            view.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            view.setContentView(R.layout.dialog_delete_custom);
            view.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            TextView tv1 = view.findViewById(R.id.tv1);
            TextView tv2 = view.findViewById(R.id.tv2);
            TextView tv3 = view.findViewById(R.id.tv3);
            tv1.setText(str1);
            tv2.setText(str2);
            tv3.setText(str3);

            if (!flag) {
                tv2.setVisibility(View.GONE);
            }
            if (onClickListener1 != null) {
                tv2.setOnClickListener(onClickListener1);
            } else {
                tv2.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        customDialog.cancel();
                    }
                });
            }
            tv3.setOnClickListener(onClickListener2);
        }
    }

    /**
     * 取消等待对话框
     */
    public void cancelDialog() {
        if (customDialog != null && customDialog.isShowing()) {
            customDialog.dismiss();
            customDialog = null;
        }
    }

}
